package com.amorcloud.pig.activity;

import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.amorcloud.pig.R;
import com.amorcloud.pig.bean.PigInfoBean;
import com.amorcloud.pig.utils.DateUtil;
import com.amorcloud.pig.utils.PigDaoUtils;

public class PigInfoDetailActivity extends BaseActivity {

    TextView tvLonTitle, tvLatTitle, tvTime, tvSex, tvLon, tvLat, tvFarmId, spLeftNipple, spRightNipple;
    Button delete;
    EditText etvSowId, etvHogId, etvBirthWeight, etvWeaningWeight;
    //Spinner spLeftNipple, spRightNipple;
    String id;
    PigInfoBean pigInfoBean;

    @Override
    protected void init() {
        setContentView(R.layout.activity_piginfo_detail);
        initCustomToolbar(this);
        initView();
        initData();
    }

    private void initView() {
        //经纬度title
        tvLonTitle = findViewById(R.id.tv_lon_title);
        tvLonTitle.setText(Html.fromHtml("<font color='#FF0000'>*</font> 经度："));
        tvLatTitle = findViewById(R.id.tv_lat_title);
        tvLatTitle.setText(Html.fromHtml("<font color='#FF0000'>*</font> 纬度："));
        //经纬度
        tvLon = findViewById(R.id.tv_lon);
        tvLat = findViewById(R.id.tv_lat);
        //时间
        tvTime = findViewById(R.id.tv_time);
        tvTime.setText(DateUtil.getYMDHMSStrDate(System.currentTimeMillis()));
        //仔猪性别
        tvSex = findViewById(R.id.tv_sex);
        //母猪场ID
        tvFarmId = findViewById(R.id.tv_farm_id);
        //母猪耳号
        etvSowId = findViewById(R.id.etv_sow_id);
        //仔猪耳号
        etvHogId = findViewById(R.id.etv_hog_id);
        //左乳头数
        spLeftNipple = findViewById(R.id.sp_left_nipple);
        //右乳头数
        spRightNipple = findViewById(R.id.sp_right_nipple);
        //分娩重量
        etvBirthWeight = findViewById(R.id.etv_birth_weight);
        //断奶重量
        etvWeaningWeight = findViewById(R.id.etv_weaning_weight);

        delete = findViewById(R.id.btn_delete_pig);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pigInfoBean != null) {
                    PigDaoUtils.delete(pigInfoBean);
                    Toast.makeText(PigInfoDetailActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                    PigInfoDetailActivity.this.finish();
                }
            }
        });
    }

    private void initData() {
        id = getIntent().getStringExtra("id");
        pigInfoBean = PigDaoUtils.queryById(id);
        if(pigInfoBean == null) {
            delete.setEnabled(false);
            return;
        }

        tvFarmId.setText(pigInfoBean.getFarmID());
        tvTime.setText(pigInfoBean.getDateTime());
        etvSowId.setText(pigInfoBean.getSow());
        etvHogId.setText(pigInfoBean.getHogID());
        tvSex.setText(pigInfoBean.getSex());
        etvBirthWeight.setText(pigInfoBean.getPrimaryW());
        etvWeaningWeight.setText(pigInfoBean.getWeaningW());
        tvLat.setText(pigInfoBean.getLat());
        tvLon.setText(pigInfoBean.getLon());
        spLeftNipple.setText(pigInfoBean.getLTeat());
        spRightNipple.setText(pigInfoBean.getRTeat());
    }
}
